﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    /// <summary>
    /// Http请求帮助类
    /// </summary>
    public class HttpHelp
    {
        /// <summary>
        /// 发送Http请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string SendHttp(string url, string data, HttpMethod method)
        {
            HttpWebRequest request = null;
            request = WebRequest.Create(url) as HttpWebRequest;
            switch (method)
            {
                case HttpMethod.Get:
                    request.Method = "GET";
                    break;
                case HttpMethod.Post:
                    {
                        request.Method = "POST";
                        byte[] bdata = Encoding.UTF8.GetBytes(data);
                        request.ContentType = "application/json;charset=UTF-8";
                        request.ContentLength = bdata.Length;
                        Stream streamOut = request.GetRequestStream();
                        streamOut.Write(bdata, 0, bdata.Length);
                        streamOut.Close();
                    }
                    break;
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamIn = response.GetResponseStream();

            StreamReader reader = new StreamReader(streamIn);
            string result = reader.ReadToEnd();
            reader.Close();
            streamIn.Close();
            response.Close();

            return result;
        }

        /// <summary>
        /// Http下载
        /// </summary>
        /// <param name="HttpFileUrl"></param>
        /// <param name="FSavePath"></param>
        /// <returns></returns>
        public static int HttpDownLoadFile(string HttpFileUrl, string FSavePath, string SaveFileName)
        {
            try
            {
                HttpFileUrl = HttpFileUrl.Replace("\\", "/");
                FSavePath = FSavePath.Replace("\\","/");
                if (!HttpFileUrl.StartsWith("http"))
                {
                    HttpFileUrl = $"https:{HttpFileUrl}";
                }
                var request = WebRequest.Create(HttpFileUrl);
                request.Method = WebRequestMethods.Http.Get;
                request.ContentType = "application/octet-stream";
                var response = request.GetResponse();
                var stream = response.GetResponseStream();
                var directPath = FSavePath;
                if (!Directory.Exists(directPath))
                {
                    Directory.CreateDirectory(directPath);
                }
                string filePath = Path.Combine(directPath, SaveFileName).Replace("\\", "/");
                var fileStream = new FileStream(filePath, FileMode.CreateNew);
                stream.CopyTo(fileStream);
                stream.Close();
                fileStream.Close();
                return 1;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }

    /// <summary>
    /// 请求类型
    /// </summary>
    public enum HttpMethod
    {
        Get = 1,
        Post = 2
    }
}
    